package apis

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"hulk-api/models/monitor"
	"fmt"
)


func GetMonitorNodes(c *gin.Context)  {
	nodes,_ := monitor.GetNodes()
	c.JSON(http.StatusOK,gin.H{
		"code": 200,
		"nodes":nodes,
	})
}

func GetHostMonitorData(c *gin.Context) {
	host := c.Query("ip")

	fmt.Println("host is ", host)

	coreTotal,memoryTotal,_ :=monitor.Host(host).CpuAndMemoryTotal()
	sysLoad,_ := monitor.Host(host).SysLoad()
	memoryUsed,_ := monitor.Host(host).MemoryUsage()
	root := monitor.Host(host).RootUsage()
	fileDescribe := monitor.Host(host).FileDescribe()
	estabAndTW := monitor.Host(host).TcpStat()

	diops,_ := monitor.Host(host).DiskIOPS()
	sdaIoRate,_ := monitor.Host(host).DiskIORate()
	diskUsage,_ := monitor.Host(host).DiskUsage()

	networkIO,_:=monitor.Host(host).NetworkIO()
	tcpConnStatus,_ := monitor.Host(host).TcpConnStat()

	c.JSON(http.StatusOK,gin.H{
		"code": 200,
		"core":coreTotal,
		"memory":memoryTotal,
		"sysload":sysLoad,
		"memoryUsed":memoryUsed.Precent,
		"root": root,
		"fileDescNum":fileDescribe,
		"tcpestabAndTW":estabAndTW,
		"diskIops":diops,
		"sdaRate": sdaIoRate,
		"diskUsage":diskUsage,
		"eth33IO" :  networkIO,
		"tcpConnStat" : tcpConnStatus,
	})
}

func GetMysqlMonitorData(c *gin.Context) {
	db := c.Query("ip")
	//base value
	uptime,_ := monitor.Mysql(db).Uptime()
	currops,_ :=monitor.Mysql(db).CurrQps()
	bufferpool,_ :=monitor.Mysql(db).BufferPoolSize()
	slowquerys,_ :=monitor.Mysql(db).CurrSlowQuerys()

	//connections
	conns,_ := monitor.Mysql(db).Connectios()
	clientThreads,_ :=monitor.Mysql(db).ClientThreads()

	//table locks
	questions,_ :=monitor.Mysql(db).Questions()
	threadCache,_ :=monitor.Mysql(db).ThreadCache()

	//abort
	abortConnections,_ :=monitor.Mysql(db).AbortConnections()
	tablelocks,_ :=monitor.Mysql(db).TableLocks()

	//network traffic
	networktraffic,_ :=monitor.Mysql(db).NetWorkTraffic()
	networkusagehourly,_ :=monitor.Mysql(db).NetWorkUsageHourly()

	//internal memory overview
	internalmemory,_ :=monitor.Mysql(db).InternalMemory()

	commands,_ := monitor.Mysql(db).Top5Commands()
	commamdshourly,_ :=monitor.Mysql(db).Top5Commandshourly()


	transactions,_ := monitor.Mysql(db).TransactionHandlers()

	c.JSON(http.StatusOK,gin.H{
		"code": 200,
		"uptime":uptime,
		"currops":currops,
		"bufferpool":bufferpool,
		"slowquerys":slowquerys,

		"connectios": conns,
		"clientthreads":clientThreads,
		"questions":questions,
		"threadcache":threadCache,
		"abortConnections":abortConnections,
		"tablelocks":tablelocks,
		"networktraffic":networktraffic,
		"networkusagehourly":networkusagehourly,
		"internalmemory":internalmemory,

		"commands":commands,
		"commamdshourly":commamdshourly,
		"transactions":transactions,


	})
}

func GetInterfaceMonitorData(c *gin.Context)  {
	fmt.Println("get interface")
	//domain := c.Query("domain")
	domain := "testing.gtljia.com"
	status,_ := monitor.Domain(domain).Status()
	c.JSON(http.StatusOK,gin.H{
		"code": 200,
		"status":status,
	})
}

func GetRestResponseCodeDistrbution(c *gin.Context)  {

	var rest monitor.RestResponseDistribution
	c.BindJSON(&rest)
	rest.CodeDistribution()
	rest.Trend()

	c.JSON(http.StatusOK,gin.H{
		"code": 200,
		"distribution":rest.Distribution,
		"codelegend" : rest.CodeSlice,
		"trend": rest.ResponseTrend,

	})
}
